package com.shensi.adminCollect.controller.station;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.adminCollect.domain.station.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 大屏展示
 * */

@RestController
@RequestMapping("/screen")
@CrossOrigin
@Slf4j
public class ScreenController {

    @Value("${shensi.httpUrl}")
    private String HTTP_URL;

    /**
     * 只有admin 和 油企可以看
     * */
    @GetMapping("/show")
    public AjaxResult getScreenData(@RequestParam(value = "enterpriseId",required = false) Long enterpriseId){
        String url = "";
        if(Objects.nonNull(enterpriseId)) {
            url = HTTP_URL + "/api/screen/show/?enterpriseId=" + enterpriseId;
        } else {
            url = HTTP_URL + "/api/screen/show/?enterpriseId=";
        }
        log.info("getScreenData智慧大屏请求地址：[{}]",url);
        String result = HttpUtil.createGet(url).execute().body();
        JSONObject jsonObject = JSONObject.parseObject(result);
        ScreenDto screenDto = new ScreenDto();
        JSONObject json =jsonObject.getJSONObject("data");

        screenDto.setAppUserNum(json.getLong("appUserNum"));
        screenDto.setConsumptionAmount(json.getBigDecimal("consumptionAmount"));
        screenDto.setRechargeAmount(json.getBigDecimal("rechargeAmount"));
        screenDto.setRechargeColumnList((List<RechargeColumnDto>) json.get("rechargeColumnList"));
        screenDto.setConsumptionFoldLineList((List<ConsumptionFoldLineDto>) json.get("consumptionFoldLineList"));
        screenDto.setOilAmtRankList((List<OilAmtRankDto>) json.get("oilAmtRankList"));
        screenDto.setChannelsList((List<ChannelsDto>) json.get("channelsList"));
        screenDto.setCountNum(json.getInteger("countNum"));
        screenDto.setTodayAppUserInfoNum(json.getLong("todayAppUserInfoNum"));
        screenDto.setRechargeUserNum(json.getLong("rechargeUserNum"));

        log.info("getScreenData智慧大屏响应结果：[{}]",screenDto);
        return AjaxResult.success(screenDto);
    }

    /**
     * 加油订单
     * 展示按时间排序查询最新的5条
     * */
    @GetMapping("/order")
    public AjaxResult getOilOrder(@RequestParam(value = "enterpriseId",required = false) Long enterpriseId){
        String url = "";
        if(Objects.nonNull(enterpriseId)) {
            url = HTTP_URL + "/api/screen/order/?enterpriseId=" + enterpriseId;
        } else {
            url = HTTP_URL + "/api/screen/order/?enterpriseId=";
        }
        log.info("getOilOrder智慧大屏请求地址：[{}]",url);
        String result = HttpUtil.createGet(url).execute().body();
        JSONObject jsonObject = JSONObject.parseObject(result);
        List<OilOrderDto> oilOrderList = (List<OilOrderDto>) jsonObject.get("data");
        log.info("getOilOrder智慧大屏响应结果：[{}]",oilOrderList);
        return AjaxResult.success(oilOrderList);
    }

}
